package com.jeecg.demo.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.jeecgframework.core.common.dao.ICommonDao;
import org.jeecgframework.core.common.dao.IGenericBaseCommonDao;
import org.jeecgframework.core.common.dao.impl.GenericBaseCommonDao;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.UploadFile;
import org.jeecgframework.core.common.model.json.ImportFile;
import org.jeecgframework.core.common.model.json.TreeGrid;
import org.jeecgframework.core.extend.template.Template;
import org.jeecgframework.core.util.PasswordUtil;
import org.jeecgframework.tag.vo.easyui.ComboTreeModel;
import org.jeecgframework.tag.vo.easyui.TreeGridModel;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.springframework.stereotype.Repository;

import com.jeecg.demo.dao.IAppUserDao;
import com.jeecg.demo.entity.AppUserEntity;
import com.jeecg.demo.entity.SysUserEntity;

@Repository
public class AppUserDao extends GenericBaseCommonDao implements IAppUserDao,
		IGenericBaseCommonDao {

	/**
	 * 检查app客户单用户是否存在
	 * */
	public SysUserEntity findAppUserByAccountAndPassword(String username,
			String password) {
		//加密客户端密码
//		String password = PasswordUtil.encrypt(username, inpassword,
//				PasswordUtil.getStaticSalt());
		String query = "from SysUserEntity u where loginname = :username and u.password=:passowrd";
		Query queryObject = getSession().createQuery(query);
		queryObject.setParameter("username", username);
		queryObject.setParameter("passowrd", password);
		@SuppressWarnings("unchecked")
		List<SysUserEntity> users = queryObject.list();
		if (users != null && users.size() > 0) {
			return users.get(0);
		}
		return null;
	}
	
	/**
	 * 修改密码
	 */
	public void updatePwd(String phoneNum,String newPwd){
		String query ="from AppUserEntity u where phoneNum = :phoneNum ";
		Query queryObject = getSession().createQuery(query);
		queryObject.setParameter("phoneNum", phoneNum);
		List<AppUserEntity> users =  queryObject.list();
		if(null != users && users.size() > 0){
			AppUserEntity user = users.get(0);
			String pwd = PasswordUtil.encrypt(user.getNickname(), newPwd, PasswordUtil.getStaticSalt());
			user.setPassword(pwd);
			save(user);
		}
	}
	
	/**
	 * 检验手机号是否唯一
	 */
	public AppUserEntity checkPhone(String phoneNum){
		String query ="from AppUserEntity u where phoneNum = :phoneNum ";
		Query queryObject = getSession().createQuery(query);
		queryObject.setParameter("phoneNum", phoneNum);
		List<AppUserEntity> users =  queryObject.list();
		if(null != users && users.size() > 0){
			return users.get(0);
		}
		return null;
	}
}
